import React, { useEffect } from 'react';
import Editor from 'wangeditor';

type Prop = {
  value: string;
  onChange: Function;
};
let editor: any = null;
const Ueditor = ({ value, onChange = () => {} }: Prop) => {
  useEffect(() => {
    if (value && editor) {
      editor.txt.html(value);
    }
  }, [value]);
  useEffect(() => {
    // 判断是否有富文本
    editor = new Editor('#editor');
    editor.config.onchange = (html: string) => {
      console.log('onchange', html);
      onChange(html);
    };
    editor.create();
    return () => {
      editor = null;
    };
  }, []);
  return (
    <div>
      <div id="editor"></div>
    </div>
  );
};

export default Ueditor;
